// map
(function(window, undefined) {
	dino = window.dino = window.dino || {};
	dino.map = {
		read: read.bind(dino.map),
		marginLeft: null,
		marginTop: null
	};
	var _instance = null;



	function read(fileName, layer) {
		var data = getFile(fileName);
		if(!data) {
			throw "no such map file!!";
		}
		var winsize = cc.Director.getInstance().getWinSize();
		var des = data.elemSize,
			dbs = data.boundSize;
		this.marginLeft = parseInt((winsize.width - des.width * dbs.width) / 2);
		this.marginTop = parseInt((winsize.height - des.height * dbs.height) / 2);
		console.log('map margin', this.marginLeft, this.marginTop);

		// background
		for(var i = 0; i < dbs.width; i++) {
			for(var j = 0; j < dbs.height; j++) {
				var bgElem = dino.create('grass');
				bgElem.setPosition(cc.p(i * des.width + this.marginLeft, j * des.height + this.marginTop));

				layer.addChild(bgElem, 3, "bg");
			};
		}



		// sprite
		var blankList = [];

		for(var i = 0; i < data.elems.length; i++) {
			var ei = data.elems[i];
			var node = dino.create(ei.name);
			node.setPosition(cc.p(ei.x * des.width + this.marginLeft, ei.y * des.height + this.marginTop));
			layer.addChild(node, 3, "sprite");
		}


	}


	// mapfile

	function getFile(fileName) {
		var data = {
			'snow': {
				boundSize: cc.SizeMake(10, 6),
				elemSize: cc.SizeMake(48, 64),
				defaultElem: 'grass',
				elems: [{
					x: 0,
					y: 0,
					name: 'chris'
				}, {
					x: 9,
					y: 2,
					name: 'tree'
				}

				]
			}
		};
		return data[fileName];
	}



})(window);